package config;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;


public class SMSConfig {
	private Properties prs=new Properties();
	private static String SMS_SERVICE_URL="sms.service.url";
	private static String SMS_SYS_CODE="sms.sys.code";
	private static String SMS_EXTENDNUMBER="sms.extendnumber";
	private static String FY_WEAK_BEFORE=	"fy.weak.before";
	public SMSConfig(){
		init();
	}
	private SMSConfig(String url,String code,String extendnumber){
		
		this.url=url;
		this.code=code;
		this.extendnumber=extendnumber;
	}
	private void init(){
		InputStream in=this.getClass().getResourceAsStream("smsservice.properties");
		
		try {
			prs.load(in);
		} catch (IOException e) {
			System.out.println("读取配置文件出错！");
			e.printStackTrace();
		}
		
	}
	
	public String getSMSServiceURL(){
		if(null==this.prs)this.init();
			return this.prs.getProperty(SMSConfig.SMS_SERVICE_URL);
	}
	
	public String getSysCode(){
		if(null==this.prs)this.init();
		return this.prs.getProperty(SMSConfig.SMS_SYS_CODE);
	}
	public String getExctenNumber(){
		if(null==this.prs)this.init();
		return this.prs.getProperty(SMSConfig.SMS_EXTENDNUMBER);
	}
	
	public String getFYWeakBeforeNum(){
		if(null==this.prs)this.init();
		return this.prs.getProperty(SMSConfig.FY_WEAK_BEFORE);
	}
	public static SMSConfig getInst(){
		return _inst;
	}
	private static SMSConfig _inst=new SMSConfig();
	
	public static void main(String[] args){
		String tmp=SMSConfig.getInst().getSMSServiceURL();
		System.out.println("tmp="+tmp);
	}
	public void clear()
    {
        this.prs.clear();
    }
	public void setValue(String key, String value)
    {
        this.prs.setProperty(key, value);
    }
	
	public void saveP(){
//		this.getClass().getResourceAsStream("smsservice.properties");
		try{
			String filePath=this.getClass().getResource("smsservice.properties").getFile();
			FileOutputStream out=new FileOutputStream(filePath.substring(1, filePath.length()));
			this.prs.store(out, "edit at :"+new Date().toLocaleString());
			out.close();
			this.prs=null;
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	private String url;
	private String code;
	private String extendnumber;
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getExtendnumber() {
		return extendnumber;
	}
	public void setExtendnumber(String extendnumber) {
		this.extendnumber = extendnumber;
	}
	
	public String doSave()throws Exception
	{
		SMSConfig sc=new SMSConfig();
        //sc.clear();
        sc.setValue(SMSConfig.SMS_SERVICE_URL, this.getUrl());
        sc.setValue(SMSConfig.SMS_SYS_CODE, this.getCode());
        sc.setValue(SMSConfig.SMS_EXTENDNUMBER, this.getExtendnumber());
        sc.saveP();
		//prs.setProperty(SMSConfig.SMS_SERVICE_URL, this.getUrl());
		System.out.println("url="+this.getUrl());
		//prs.setProperty(SMSConfig.SMS_SYS_CODE, this.getCode());
		System.out.println("getCode="+this.getCode());
		//prs.setProperty(SMSConfig.SMS_EXTENDNUMBER, this.getExtendnumber());
		System.out.println("getExtendnumber="+this.getExtendnumber());
		return "success";
	}
	
	public String doDetail(){
		SMSConfig sc=new SMSConfig();
		sc.init();
		this.url=SMSConfig.getInst().getSMSServiceURL();
		this.code=SMSConfig.getInst().getSysCode();
		this.extendnumber=SMSConfig.getInst().getExctenNumber();
		return "success";
	}
	
}
